﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class QuickLMSCourseLaunch : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
            LoadCourseList();
    }

    public void LoadCourseList()
    {
        System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Server.MapPath("~/Courses/"));
        CourseList.DataSource = di.GetDirectories();
        CourseList.DataBind();
    }

    protected void LaunchCourseWeb(object sender, EventArgs e)
    {
        Response.Redirect("QuickLMSNav.aspx?NT=W&FN=" + CourseList.SelectedValue);
    }

    protected void LaunchCourseSL(object sender, EventArgs e)
    {
        Response.Redirect("QuickLMSNav.aspx?NT=SL&FN=" + CourseList.SelectedValue);
    }

    protected void LoadZipFile(object sender, EventArgs e)
    {
        byte[] buffer = null;
        bool dc = false;

        try
        {
            //System.Text.ASCIIEncoding ae = new System.Text.ASCIIEncoding();
            //System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath("E500_11.zip"), System.IO.FileMode.Open);
            ICSharpCode.SharpZipLib.Zip.ZipInputStream zis = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(new System.IO.MemoryStream(fuLMSFile.FileBytes));
            ICSharpCode.SharpZipLib.Zip.ZipEntry ze = null;
            while (true)
            {
                ze = zis.GetNextEntry();
                if (ze == null)
                    break;
                //Response.Write(ze.Name + ":" + ze.IsDirectory + "<br>");
                if (ze.IsDirectory)
                {
                    System.IO.Directory.CreateDirectory(Server.MapPath("~/Courses/") + fuLMSFile.FileName.Replace(".zip", "") + @"\" + ze.Name);
                    dc = true;
                }
            }

            if (!dc)
                System.IO.Directory.CreateDirectory(Server.MapPath("~/Courses/") + fuLMSFile.FileName.Replace(".zip", "") + @"\");

            zis.Close();
            zis = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(new System.IO.MemoryStream(fuLMSFile.FileBytes));

            while (true)
            {
                System.IO.FileStream fs = null;
                ze = zis.GetNextEntry();
                if (ze == null)
                    break;
                if (!ze.IsDirectory)
                {
                    fs = new System.IO.FileStream(Server.MapPath("~/Courses/") + fuLMSFile.FileName.Replace(".zip", "") + @"\" + ze.Name, System.IO.FileMode.Create);
                    buffer = new byte[(int)ze.Size];
                    zis.Read(buffer, 0, (int)ze.Size);
                    fs.Write(buffer, 0, buffer.Length);
                    fs.Close();
                }
            }
            zis.Close();
            lblStatus.ForeColor = System.Drawing.Color.DarkGreen;
            lblStatus.Text = "Cartridge Loaded Successfully";
            LoadCourseList();
        }
        catch (Exception E)
        {
            lblStatus.Text = E.Message;
            lblStatus.ForeColor = System.Drawing.Color.DarkRed;
        }
    }
}
